<?php

namespace app\controllers;

use yii\web\Controller;
use yii\data\Pagination;
use app\models\Pr_project;
use app\models\Pr_like;
use Yii;

class ProjectController extends Controller
{
    //禁用原有布局
    public $layout="main2";
    //显示
    public function actionShow($status = 'all')
    {

        RegisterController::prepare();


        $projects = $this->getProject($status);
        return $this->render('project',[
            'projects' => $projects,
            'status' => $status
            ]);
    }
    //显示项目概况
    public function actionShowDetail($pr_id)
    {

        RegisterController::prepare();


        $project = Pr_project::findOne([
            'pr_id' => $pr_id,
            'del_flag' => 0
            ]);
        $project->read_num += 1;
        $project->save();
        return $this->render('project-detail',[
            'project' => $project
            ]);
    }
    //关注项目
    public function actionLike($pr_id)
    {   
        if(!isset($_COOKIE['user_id']))
            $user_id = 0;
        else
            $user_id = $_COOKIE['user_id'];
        $like = new Pr_like();
        $like->user_id = $user_id;
        $like->project_id = $pr_id;
        $like->del_flag = 0;
        $flag = true;
        if(!Pr_like::findOne([
            'user_id' => $user_id,
            'project_id' => $pr_id,
            'del_flag' => 0
            ]))
        {
            $like->save();
        }
        else
        {
            $flag = false;
        }
        $project = Pr_project::findOne([
            'pr_id' => $pr_id,
            'del_flag' => 0
            ]);
        if(true == $flag)
        {
        $project->like_num += 1;
        $project->save();
        }
        return $this->render('project-detail',[
            'project' => $project
            ]);
    }
    //创业项目数据
    public function getProject($status = 'all')
    {    
        //全部项目
        if('all' == $status)
        {
            $query = Pr_project::find()
            ->where([
                'status' => [0, 1, 2],
                'del_flag' => 0
                ]);
        }
        //未开始
        else if('prepare' == $status)
        {
            $query = Pr_project::find()
            ->where([
                'status' => 0,
                'del_flag' => 0
                ]);
        }
        //招募中
        else if('recruit' == $status)
        {
            $query = Pr_project::find()
            ->where([
                'status' => 1,
                'del_flag' => 0
                ]);
        }
        //已过期
        else if('pastDue' == $status)
        {
            $query = Pr_project::find()
            ->where([
                'status' => 2,
                'del_flag' => 0
                ]);
        }
        $pagination = new Pagination([
            'defaultPageSize' => 12,
            'totalCount' => $query->count()
            ]);
        $projects = $query->orderBy('pr_id DESC')
        ->offset($pagination->offset)
        ->limit($pagination->limit)
        ->all();

        return [
        'projects' => $projects,
        'pagination' => $pagination,
        'total' => $query->count()
        ];
    }
}